package com.cheche.dn.domain.admin;

import com.cheche.dn.common.base.BaseEntity;
import com.cheche.dn.infrastructure.validator.IntType;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Sets;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import java.util.Set;

/**
 * Entity - Admin
 * Created by cheshun on 15/8/21.
 */
@Entity
@Table(name = "admin")
public class Admin extends BaseEntity {
    private static final long serialVersionUID = 1695951661939856261L;

    /** "用户名"属性名 */
    public static final String USERNAME_PROPERTY_NAME = "username";

    private String username;        // 用户名
    private String password;        // 密码
    private String nickname;        // 昵称
    /** 角色 */
    private Set<Role> roles = Sets.newHashSet();

    @JsonProperty
    @NotEmpty
    @Column(length = 30, unique = true)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Length(min = 6, max = 128)
    @Column(length = 128)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @JsonProperty
    @Column(length = 50)
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * 获取角色
     *
     * @return 角色
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "admin_role")
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}
